<template>
    <AgencyRegister :dialogForm="agencyInfo" />
    <div class="btn-group">
        <el-button type="primary" @click="handleSubmit">确认修改</el-button>
    </div>
</template>

<script lang="ts" setup>
import { dayjs, ElMessage } from "element-plus";
import { onMounted, ref } from "vue";
import AgencyRegister from "./AgencyRegister.vue";
import { adminApi } from "../../api/app";

const agencyInfo = ref({
    agencyId: null,
    agencyName: null,
    agencyLogo: null,
    agencyEmail: null,
    agencyPhone: null,
    agencyStartTime: null,
    agencyWebsite: null,
    agencyFax: null,
    agencyIntroduction: null,
    registrationAdministrationAuthority: null,
    registrationCertificateType: null,
    registrationCertificatePhoto: null,
    registrationCertificateAddress: null,
    socialCreditCode: null,
    organizationRating: null,
    agencyNature: null,
    staffsNumber: null,
    volunteersNumber: null,
    corporateName: null,
    corporateId: null,
    bussinessScope: null,
    certificateTimes: null,
    agencyAddress: null,
    agencyPassword: null,
    orgLegalIdCardIsLongTerm: false, // 证书有效期是否长期
    address: [], // 地址
});

onMounted(() => {
    const agency = JSON.parse(localStorage.getItem('user') || "");
    const allAddress = agency.registrationCertificateAddress.split('/');
    const address = allAddress.splice(0, 3);
    const certificateTimes = agency.certificateTimes.split('~');
    const orgLegalIdCardIsLongTerm = !(certificateTimes.length > 1);
    agencyInfo.value = {
        ...agency,
        organizationRating: agency.organizationRating.split('/'),
        certificateTimes: orgLegalIdCardIsLongTerm ? new Date(certificateTimes[0]) : [new Date(certificateTimes[0]), new Date(certificateTimes[1])],
        address,
        registrationCertificateAddress: allAddress.toString(),
        agencyStartTime: new Date(agency.agencyStartTime)
    };
})

/** 提交表单 */
const handleSubmit = () => {
    // 处理地址存储格式
    const registrationCertificateAddress = agencyInfo.value.address.join('/') + '/' + agencyInfo.value.registrationCertificateAddress;
    // 处理时间格式
    let certificateTimes: string;
    // 长期有效，直接转换，短期有效，拼接
    if (agencyInfo.value.orgLegalIdCardIsLongTerm) {
        certificateTimes = dayjs(agencyInfo.value.certificateTimes).format('YYYY-MM-DD');
    } else {
        certificateTimes = dayjs(agencyInfo.value.certificateTimes?.[0]).format('YYYY-MM-DD') + '~' + dayjs(agencyInfo.value.certificateTimes?.[1]).format('YYYY-MM-DD');
    }
    const agency = {
        ...agencyInfo.value,
        address: null,
        registrationCertificateAddress: registrationCertificateAddress,
        orgLegalIdCardIsLongTerm: undefined,
        certificateTimes: certificateTimes,
        organizationRating: (agencyInfo.value.organizationRating || []).join('/'),
        agencyStartTime: dayjs(agencyInfo.value.agencyStartTime).format('YYYY-MM-DD'),
        role: undefined
    }
    adminApi.updateAgency(agency).then(() => {
        ElMessage.success('修改成功');
        // 更新本地缓存
        localStorage.removeItem('user');
        localStorage.setItem('user', JSON.stringify({...agency, role: 'agency'}));
    })
};
</script>

<style scoped lang="less">
* {
    padding: 20px;

    .btn-group {
        display: flex;
        justify-content: flex-end;
    }
}
</style>